package com.tzxs.pay.main.onlineaut.fragment;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.tzxs.pay.BR;
import com.tzxs.pay.R;
import com.tzxs.pay.databinding.FragmentOnlineAutListBinding;
import com.tzxs.pay.databinding.ItemOnlineAutBinding;
import com.tzxs.pay.main.BaseFragment;
import com.tzxs.pay.main.bill.viewmodel.BillViewModel;
import com.tzxs.pay.main.onlineaut.OnlineAdActivity;
import com.tzxs.pay.main.onlineaut.broadcast.AutFinishBroadCast;
import com.tzxs.pay.main.onlineaut.model.OnlineAutBean;
import com.tzxs.pay.util.IAdapter;
import com.tzxs.pay.util.IHolder;

import java.util.List;

/**
 * 在线预授权列表
 */
public class OnlineAutListFragment extends BaseFragment<FragmentOnlineAutListBinding> implements IAdapter.ItemNavigator<OnlineAutBean.DataBean> {
    private static final String TAG = OnlineAutListFragment.class.getSimpleName();
    private BillViewModel model;

    //是否初始化完成
    private boolean isIninted = false;
    private int page = 1;


    @Override
    public int getContentLayoutId() {
        return R.layout.fragment_online_aut_list;
    }

    @Override
    protected void viewCreate(Bundle savedInstanceState) {

        isIninted = true;
        model = ViewModelProviders.of(this).get(BillViewModel.class);
        binding.setViewModel(model);
        binding.autList.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.autList.setAdapter(new IAdapter<OnlineAutBean.DataBean, ItemOnlineAutBinding>() {


            @Override
            protected int getLayoutId(int viewType) {
                return R.layout.item_online_aut;
            }


            @Override
            protected int getDataBRId(int itemViewType) {
                return BR.item;
            }


            @Override
            protected void convertListener(@NonNull ItemOnlineAutBinding binding) {
                binding.setItemNavigator(OnlineAutListFragment.this);
            }

            @Override
            public void onBindViewHolder(IHolder<ItemOnlineAutBinding> holder, int position) {
                super.onBindViewHolder(holder, position);
                OnlineAutBean.DataBean bean = getData().get(position);
                switch (bean.getPay_status()) {
                    case 1:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#1F78CF"));
                        break;
                    case 2:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#999999"));
                        break;
                    case 3:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#27AA30"));
                        break;
                    case 4:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#ED433F"));
                        break;
                    case 5:
                        holder.binding.payStatus.setTextColor(Color.parseColor("#EB9205"));
                        break;
                }
                holder.binding.payStatus.setText(bean.getStatusTypeText());
            }

            @Override
            public boolean areItemsTheSame(List<OnlineAutBean.DataBean> oldData, List<OnlineAutBean.DataBean> data, int oldItemPosition, int newItemPosition) {
                return oldData.get(oldItemPosition).getOut_order_no().equals(data.get(newItemPosition).getOut_order_no());
            }
        });

        //
        model.snackbarText.observe(this, s -> {
            if (s != null) {
                Snackbar.make(binding.autList, s, Snackbar.LENGTH_SHORT).show();
            }
        });
      /*  if (getUserVisibleHint()) {
            model.getOnlineAutList(1, 10, () -> {
                //获取数据
            });
        }*/


        //下拉刷新
        binding.refresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            model.getOnlineAutList(page, 10, () -> {
                //获取数据成功
                refreshLayout.finishRefresh(1000);
            });

        });
        binding.refresh.setOnLoadMoreListener(refreshLayout -> {
            ++page;
            model.getOnlineAutList(page, 10, () -> {
                //获取数据成功
                refreshLayout.finishLoadMore(1000);
            });

        });
    }

    //设置懒加载
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && isIninted) {
            model.getOnlineAutList(1, 10, () -> {
                Log.d(TAG, "setUserVisibleHint: ");
            });
            isIninted = false;
        }
    }


    //item  单击事件
    @Override
    public void itemClick(OnlineAutBean.DataBean item) {

        Intent intent = new Intent(getContext(), OnlineAdActivity.class);
        intent.putExtra("orderId", item.getOut_order_no());
        startActivity(intent);
    }
}
